﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Principal;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Cofe.Core.Security
{
    public class CofeImpersonationContext : IDisposable
    {
        private static int _ctr = 0;
        public static int LockCount { get { return _ctr; } }

        internal CofeImpersonationContext(IIdentity identity)
        {
            Interlocked.Increment(ref _ctr);
            CofeServices.SecurityManager.setIdentity(identity);
            //Thread.CurrentPrincipal = CofeServices.SecurityManager.UserManager.GetPrincipal(identity);
        }

        public void Dispose()
        {
            CofeServices.SecurityManager.setIdentity(null);
            //(CofeServices.SecurityManager as SecurityManager).unsetIdentity();
            //Thread.CurrentPrincipal = CofeServices.SecurityManager.UserManager.GetPrincipal();
            Interlocked.Decrement(ref _ctr);
        }
    }
}
